package drds.server.net.packet;

import java.nio.ByteBuffer;

import drds.server.buffer.ByteBufferList;
import drds.server.net.SessionContext$LifeCycle;

/**
 * From Server To Client, part of Result Set Packets. One for each column in the
 * result set. Thus, if the value of field_columns in the Result Set Header
 * Packet is 3, then the Field Packet occurs 3 times.
 * 
 * <pre>
 * Bytes                      Name
 * -----                      ----
 * n (Length Coded String)    catalog
 * n (Length Coded String)    db
 * n (Length Coded String)    table
 * n (Length Coded String)    org_table
 * n (Length Coded String)    name
 * n (Length Coded String)    org_name
 * 1                          (filler)
 * 2                          charsetNumber
 * 4                          length
 * 1                          type
 * 2                          flags
 * 1                          decimals
 * 2                          (filler), always 0x00
 * n (Length Coded Binary)    default
 * 
 * @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Field_Packet
 * </pre>
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class FieldPacket extends Packet {
	private static final byte[] FILLER = new byte[2];

	public byte[] name;
	public long length;
	public int type;
	public int flags;
	public byte decimals;

	/**
	 * 把字节数组转变成FieldPacket
	 */
	public void read(byte[] bytes) {
		BytesHolder bytesHolder = new BytesHolder(bytes);
		this.packetLength = bytesHolder.readInt3();
		this.packetId = bytesHolder.read();
		readBody(bytesHolder);
	}

	/**
	 * 把BinaryPacket转变成FieldPacket
	 */
	public void read(BinaryPacket binaryPacket) {
		this.packetLength = binaryPacket.packetLength;
		this.packetId = binaryPacket.packetId;
		readBody(new BytesHolder(binaryPacket.bytes));
	}

	@Override
	public ByteBuffer write(ByteBuffer byteBuffer, SessionContext$LifeCycle sessionContext, boolean writeSocketIfFull) {
		int size = computePacketSize();
		byteBuffer = sessionContext.checkWriteByteBuffer(byteBuffer, sessionContext.getPacketHeaderSize() + size, writeSocketIfFull);
		ByteBufferUtil.writeInt3(byteBuffer, size);
		byteBuffer.put(packetId);
		writeBody(byteBuffer);
		return byteBuffer;
	}

	@Override
	public int computePacketSize() {
		int size = 0;
		size += (name == null ? 1 : ByteBufferUtil.getLength(name));
		size += 13;// 1+2+4+1+2+1+2
		return size;
	}

	private void readBody(BytesHolder bytesHolder) {
		this.name = bytesHolder.readBytesWithLength();
		this.length = bytesHolder.readLong4();
		this.type = bytesHolder.read() & 0xff;
		this.flags = bytesHolder.readInt2();
		this.decimals = bytesHolder.read();
		bytesHolder.move(FILLER.length);
	}

	private void writeBody(ByteBuffer byteBuffer) {
		byte nullVal = 0;
		ByteBufferUtil.writeWithLength(byteBuffer, name, nullVal);
		byteBuffer.put((byte) 0x0C);
		ByteBufferUtil.writeLong4(byteBuffer, length);
		byteBuffer.put((byte) (type & 0xff));
		ByteBufferUtil.writeInt2(byteBuffer, flags);
		byteBuffer.put(decimals);
		byteBuffer.put((byte) 0x00);
		byteBuffer.put((byte) 0x00);
		// buffer.position(buffer.position() + FILLER.length);
	}

	public void write(ByteBufferList byteBufferList) {
		int size = computePacketSize();
		ByteBuffer byteBuffer = byteBufferList.checkWritingByteBufferCapacity(packetHeaderSize + size);
		ByteBufferUtil.writeInt3(byteBuffer, size);
		byteBuffer.put(packetId);
		writeBody(byteBuffer);
	}

}